// diff算法的patch()方法

// 1.初始化 patch(container,vnode)
// 2.更新 update(vnode,newVnode)

function createElement(vnode) {
  let tag = vnode.tag; // 目标元素
  let attrs = vnode.attrs || {}; //属性
  let children = vnode.children || []; //子节点

  if (!tag) {
    return null;
  }
  //   创建对应的dom
  let elem = document.createElement(tag);
  let attrName;
  //   给dom添加属性
  for (attrName in attrs) {
    //
    if (attrs.hasOwnProperty(attrName)) {
      elem.setAttribute(attrName, attrs[attrName]);
    }
  }
  //   将子元素添加到目标元素之上
  children.forEach((childVnode) => {
    elem.appendChild(createElement(childVnode));
  });

  return elem;
}

function updateChildren(vnode, newVnode) {
  let children = vnode.children || [];
  let newChildren = newVnode.children || [];

  children.forEach((childrenVnode, index) => {
    let newChildrenVnode = newChildren[index];
    // 第一层有无变化
    if (childrenVnode.tag === newChildrenVnode.tag) {
      // 再往下走递归下去
      updateChildren(childrenVnode, newChildrenVnode);
    } else {
      // vue内部方法
      replaceNode(childrenVnode, newChildrenVnode);
    }
  });
}
